package com.epalmpay.job;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.epalmpay.entity.TradeOffline;
import com.epalmpay.enumdef.CommonEnum.RunStatus;
import com.epalmpay.enumdef.CommonEnum.TaskType;
import com.epalmpay.service.jobservice.IProfitStatisService;
import com.epalmpay.service.operator.ISysLogService;
import com.epalmpay.util.DateUtil;
import com.epalmpay.util.Logger;

@Component
public class ProfitDayRefundJob {
     
	@Resource
	private ISysLogService sysLogService;
	@Resource
	private IProfitStatisService profitStatisService;
	
	protected Logger logger = Logger.getLogger(this.getClass());	
	
	/**
	 * 分润日退款
	 * 每天凌晨4点、10点、16点、22点5分执行一次
	 * 对有失败的任务可通过
	 */
	@Scheduled(cron="0 5 4,10,16,22 * * ?")
	public void  run(){

		//统计日期
		Date statisDate = null;
		StringBuffer sb = new StringBuffer(); 
		try {			
			//取得当前日期yyyyMMdd
//			statisDate = DateUtil.fomatDate(DateUtil.fomatDate(new Date(), DateUtil.sdfDays), DateUtil.sdfDays);
//
//			List<TradeOffline> records = profitStatisService.selectNeedProfitRefundList(null);
//			//循环处理单笔交易分润
//			for (TradeOffline record : records) {
//				try{
//					profitStatisService.saveOneOrderProfitRefund(record);
//				} catch(Exception e){
//					sysLogService.recordSysTaskLog(TaskType.ProfitDayRefund.getType(),
//							statisDate, RunStatus.Fail.getType(),
//							sb.append("订单号:").append(record.getId()).append("分润退款失败！").toString());
//					throw e;
//				}
//			}
//			logger.info(sb.append("计划任务:")
//					.append(TaskType.ProfitDayRefund.getName())
//					.append("统计日期:").append(DateUtil.fomatDate(statisDate, DateUtil.sdfDays)).append("执行成功！"));
//			sysLogService.recordSysTaskLog(TaskType.ProfitDayRefund.getType(),
//					statisDate, RunStatus.Success.getType(),
//					sb.append("分润退款").append(DateUtil.fomatDate(new Date(), DateUtil.sdfTime)).append("执行成功！").toString());
		}		
		 catch (Exception e) { 
			e.printStackTrace();
			logger.info(sb.append("计划任务:")
					.append(TaskType.ProfitDayRefund.getName())
					.append("统计日期：").append(DateUtil.fomatDate(statisDate, DateUtil.sdfDays)).append("执行失败！"));			
			logger.error(e);
		}
	}

}
